<?php

namespace App\Http\Controllers;

use App\Admin;
use Illuminate\Http\Request;
use App\Club;
use Illuminate\Support\Facades\Hash;

class AdminController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, Admin $admin)
    {
        $count = collect([
            'all' => $admin->count(),
            'status0' => $admin->where('status', 0)->count(),
            'status1' => $admin->where('status', 1)->count()
        ]);

        if (is_null($request->search)) {
            $admin = is_null($request->type) ? $admin->paginate(10) : $admin->where('status', $request->type)->get();
        } else {
            $admin = $admin->where('username', 'like', "%{$request->search}%")
                ->orWhere('tel', 'like', "%{$request->search}%")
                ->paginate(10);
        }

        foreach ($admin as $a) {
            $a->clubname = $a->club->club_name;
        }

        return view('admins.index', [
            'admins' => $admin,
            'all' => $count->get('all'),
            'status0' => $count->get('status0'),
            'status1' => $count->get('status1')
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $club = Club::all();
        return view('admins.create', compact('club'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|min:3|max:255',
            'tel' => [
                'required',
                'digits:11',
                'regex:/^1[34578][0-9]{9}$/'
            ],
            'password' => 'required|alpha_dash|min:6|max:18|confirmed'
        ]);
        Admin::create([
            'username' => $request->name,
            'password' => Hash::make($request->password),
            'tel' => $request->tel,
            'club_id' => $request->club,
            'status' => 0,
            'remember_token' => $request->_token
        ]);

        return redirect('admins')->with('message', '新增俱乐部管理员成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Admin  $admins
     * @return \Illuminate\Http\Response
     */
    public function show(Admin $admins)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Admin  $admins
     * @return \Illuminate\Http\Response
     */
    public function edit(Admin $admins)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Admin  $admins
     * @return \Illuminate\Http\Response
     */
    public function update($id)
    {
        $admins = Admin::find($id);
        $admins->status == 1 ? $admins->status -= 1 : $admins->status += 1;
        $admins->save();
        return redirect('admins');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Admin  $admins
     * @return \Illuminate\Http\Response
     */
    public function destroy(Admin $admins)
    {
        //
    }
}
